class TFWBFire extends WeaponFire;

var TFWBlade blade;
var TFWBladeGlow glow;
var float lastmark, activationtime, dmgremain;
var Vector MarkLocation, effectoffset, oldPos;
var() float TraceRange;
var sound HitSound[4], HitwallSound[4];
var sound BladeHum, ActivationSound;


/*replication
{
    reliable if ( bNetOwner && (ROLE==ROLE_Authority) )
        oldPos, lastmark, activationtime, dmgremain;
}*/

state Cut
{
    function BeginState()
    {
        activationtime=level.TimeSeconds;

        Weapon.PlayOwnedSound(sound'TFMedia.WarpBlade.tfwbactivate',,1.5);
    }

    function ModeTick(float tick)
    {
        local Vector start, StartTrace, EndTrace, X,Y,Z;
        local Vector HitLocation, HitNormal, diff;
        local Actor Other, other2;
        local Rotator Aim;
        local xWeaponAttachment Attachment;
        local float dist, dmg;
        local tfwbladefx fx;
        local emitter slice;
        local int i;





        Weapon.GetViewAxes(X,Y,Z);
        StartTrace = Instigator.Location + Instigator.EyePosition() + X*Instigator.CollisionRadius;
        Aim = AdjustAim(StartTrace, AimError);
        X = Vector(Aim);
        EndTrace = StartTrace + TraceRange * X;
        Other = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);

        if(oldpos==vect(0,0,0) || blade==none) oldpos=endtrace;
        else oldpos=blade.mSpawnVecA;
        diff=(endtrace-oldpos)*tick;


        if(other==none) other = Trace(HitLocation, HitNormal, starttrace+tracerange*normal(diff/2+oldpos-starttrace), starttrace, true);
        if(other==none) other = Trace(HitLocation, HitNormal, Endtrace, oldpos, true);

        Attachment = xPawn(Instigator).WeaponAttachment;
        if ( Instigator.IsFirstPerson() || aicontroller(Instigator.controller)!=none)
        {
			if ( Weapon.WeaponCentered() )
				start=Instigator.Location;
			else
			{
				start=Instigator.Location + Instigator.CalcDrawOffset(Weapon) + EffectOffset.X * X + Instigator.Weapon.Hand * EffectOffset.Y * Y + EffectOffset.Z * Z;
			}
        }
        else if (Attachment != none && (Level.TimeSeconds - Attachment.LastRenderTime) < 1)
                start=Attachment.GetTipLocation()-vector(aim)*20;


        if (blade == none && level.TimeSeconds-activationtime > 0.25)
        {
            blade = Spawn(class'TFWBlade',,,start);
            blade.mSpawnVecA=endtrace;
        }
        if (glow == none && level.TimeSeconds-activationtime < 0.2)
        {
            glow = Spawn(class'TFWBladeGlow',,,start);
        }


        if (!bIsFiring)
        {
            if(blade!=none) blade.Destroy();
            if(glow!=none) glow.Destroy();
            StopFiring();
        }






        if(blade!=none)
        {
            weapon.AmbientSound=Sound'TFMedia.Warpblade.tfwbhum';
            blade.SetLocation(start);
            blade.setrotation(rotator(Normal(EndTrace - start)));
            blade.mSpawnVecA=endtrace;
        }
        if(glow!=none)
        {
            glow.SetLocation(start);
            glow.setrotation(rotator(Normal(EndTrace - start)));
        }

        if(level.TimeSeconds-activationtime > 0.2 && glow!=none) glow.mRegen=false;
        if(level.TimeSeconds-activationtime < 0.25) return;

        dmg=1500*tick;
        dmg+=dmgremain;
        dmgremain=dmg-int(dmg);
        dmg=int(dmg);


        if (Other != Instigator)
        {
            weapon.soundpitch=fmax(0,fmin(255,50+(vsize(diff/tick)/2)));
            blade.soundpitch=fmax(0,fmin(255,50+(vsize(diff/tick)/2)));

            if(pawn(other)==none) dmg/=2.5;

            if(other!=none) Other.TakeDamage( dmg, Instigator, other.Location, diff*5, class'Taskforces.DamTypeWarpblade' );

            if(level.TimeSeconds-lastmark > 0.03)
			{
                for(i=0; i<vsize(diff/tick)/14-0.5; i++)
                {
                    //if(EffectIsRelevant(start+normal((endtrace-(diff/tick))-start)*(tracerange/2),false))
                    //{
                        slice=Spawn(class'taskforces.tfwbslicefx',,,start+normal((endtrace-(diff/tick))-start)*(tracerange/2));
                        slice.Emitters[0].RotationOffset=rotator(normal((endtrace-(diff/tick))-start));
                        slice=Spawn(class'taskforces.tfwbslicefx',,,start+normal((endtrace-(diff/tick)*2)-start)*(tracerange/2));
                        slice.Emitters[0].RotationOffset=rotator(normal((endtrace-(diff/tick)*2)-start));

                        if(!Level.bDropDetail && Level.DetailMode != DM_Low)
                        {
                            slice=Spawn(class'taskforces.tfwbslicefx',,,start+normal((endtrace-(diff/tick)*0.5)-start)*(tracerange/2),rotator(normal((endtrace-(diff/tick)*0.5)-start)));
                            slice.Emitters[0].RotationOffset=rotator(normal((endtrace-(diff/tick)*0.5)-start));
                            slice=Spawn(class'taskforces.tfwbslicefx',,,start+normal((endtrace-(diff/tick)*1.5)-start)*(tracerange/2),rotator(normal((endtrace-(diff/tick)*1.5)-start)));
                            slice.Emitters[0].RotationOffset=rotator(normal((endtrace-(diff/tick)*1.5)-start));
                        }
                    //}
                }

                if(other!=none)
                {
                    //if(EffectIsRelevant(hitLocation+hitnormal*4,false))
                        Spawn(class'taskforces.tfwbhitsparkle',,,hitLocation+hitnormal*4,rotator(hitnormal));

                    if(other.bWorldGeometry)
                    {
                        //if(EffectIsRelevant(hitLocation-vector(Aim)*4,false))
                            Spawn(class'taskforces.tfwbdecal',,,hitLocation-vector(Aim)*4,aim);
                        if(frand()<0.6) Weapon.PlaySound(HitwallSound[rand(4)],SLOT_None,vsize(diff/tick)/10+0.2,,,0.9+frand()*0.2);
                    }
                    else
                    {
                        if(frand()<0.6) Weapon.PlaySound(HitSound[rand(4)],SLOT_None,vsize(diff/tick)/10+0.2,,,0.9+frand()*0.2);
                    }
                }
                lastmark=level.TimeSeconds;
            }
        }
    }

    function StopFiring()
    {


        oldpos=vect(0,0,0);
        if(blade!=none) blade.Destroy();
        if(glow!=none) glow.Destroy();
        weapon.AmbientSound=none;
        Weapon.PlayOwnedSound(sound'TFMedia.WarpBlade.tfwbdeactivate',,1.5);
        GotoState('');
    }

    function EndState()
    {
    }
}

function DoFireEffect()
{
}

function ModeHoldFire()
{
    GotoState('Cut');
}

function StartBerserk()
{
}

function StopBerserk()
{
}

function vector GetFireStart(vector X, vector Y, vector Z)
{
    return Instigator.Location + Instigator.EyePosition() + X*Instigator.CollisionRadius;
}

defaultproperties
{
     EffectOffset=(X=36.000000,Y=11.000000,Z=-10.000000)
     TraceRange=165.000000
     HitSound(0)=Sound'TFMedia.Warpblade.TFWBhit1'
     HitSound(1)=Sound'TFMedia.Warpblade.TFWBhit2'
     HitSound(2)=Sound'TFMedia.Warpblade.TFWBhit3'
     HitSound(3)=Sound'TFMedia.Warpblade.TFWBhit4'
     HitwallSound(0)=Sound'TFMedia.Warpblade.TFWBhitwall1'
     HitwallSound(1)=Sound'TFMedia.Warpblade.TFWBhitwall2'
     HitwallSound(2)=Sound'TFMedia.Warpblade.TFWBhitwall3'
     HitwallSound(3)=Sound'TFMedia.Warpblade.TFWBhitwall4'
     bFireOnRelease=True
     FireEndAnim=
     FireRate=0.750000
     AmmoClass=Class'Taskforces.TFWBAmmo'
     BotRefireRate=0.990000
     WarnTargetPct=0.100000
}